//
//  YingDanCollectionViewCell.m
//  C17
//
//  Created by dllo on 16/3/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YingDanCollectionViewCell.h"
#import "YingDanTableViewCell.h"
#import "movieThirdCollectionViewCell.h"
#import "YingDanViewController.h"

@interface YingDanCollectionViewCell ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *arr;
//加载数据用的自加数字
@property(nonatomic,assign)NSInteger count;
@end

@implementation YingDanCollectionViewCell

- (void)awakeFromNib {
    // Initialization code
    [self getData];
    //集成刷新控件
    [self setupRefresh];
    self.count=2;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YingDanTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"tableView"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YingDanTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"tableView" forIndexPath:indexPath];
    NSDictionary *dic=self.arr[indexPath.row];
    cell.name.text=dic[@"name"];
    NSString *str=[NSString stringWithFormat:@"%@",dic[@"movie_count"]];
    cell.shuliang.text=str;
//    cell.zuozhe.text=dic[@"user"][@"name"];
//    cell.renshu.text=[NSString stringWithFormat:@"%@",dic[@"follow_count"]];
    NSString *renshu=[NSString stringWithFormat:@"%@",dic[@"follow_count"]];
    NSString *jieshao=[NSString stringWithFormat:@"%@创建/%@人关注该电影",dic[@"user"][@"name"],renshu];
    cell.zuozhe.text=jieshao;
//    cell.movieThirdcollectionView.delegate=self;
//    cell.movieThirdcollectionView.dataSource=self;
    cell.pagelist_id=dic[@"pagelist_id"];
    NSInteger page=[dic[@"movie_count"] integerValue]/20+1;
    cell.page=page;
    return cell;
}


-(void)getData{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager POST:@"http://ting.weibo.com/movieapp/Pagelist/recommend" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.arr = [NSMutableArray arrayWithArray:responseObject[@"data"][@"list"]];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
}

#pragma mark 刷新newsTableView
-(void)setupRefresh{
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"table"];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
}
-(void)headerRereshing{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager POST:@"http://ting.weibo.com/movieapp/Pagelist/recommend" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSMutableArray *arr=responseObject[@"data"][@"list"];
        self.arr = [NSMutableArray arrayWithArray:arr];
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    [self.tableView headerEndRefreshing];
}
-(void)footerRereshing{
    NSString *page=[NSString stringWithFormat:@"%ld",self.count];
    NSDictionary *parameters = @{@"page":page};
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager POST:@"http://ting.weibo.com/movieapp/Pagelist/recommend" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSMutableArray *arr=responseObject[@"data"][@"list"];
        for (NSDictionary *dic in arr) {
            [self.arr addObject:dic];
        }
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    [self.tableView footerEndRefreshing];
    self.count+=1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic=self.arr[indexPath.row];
    YingDanViewController *vc=[[YingDanViewController alloc] init];
    vc.pagelist_id=dic[@"pagelist_id"];
    // 进入后隐藏tabbar
    vc.hidesBottomBarWhenPushed=YES;
    [self.root.navigationController pushViewController:vc animated:YES];
    // 退出时显示tabbar
    self.root.hidesBottomBarWhenPushed = NO;
}



@end
